import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ProductListPage extends JPanel {
    public ProductListPage(CardLayout cardLayout, JPanel cardPanel) {
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);

        // 顶部搜索栏
        JPanel topPanel = createTopPanel();
        add(topPanel, BorderLayout.NORTH);

        // 商品网格
        JPanel productGrid = createProductGrid(cardLayout, cardPanel);
        JScrollPane scrollPane = new JScrollPane(productGrid);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        add(scrollPane, BorderLayout.CENTER);

        // 底部导航栏
        JPanel bottomNav = createBottomNav();
        add(bottomNav, BorderLayout.SOUTH);
    }

    private JPanel createTopPanel() {
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
        topPanel.setBackground(new Color(240, 240, 240));
        
        // 搜索框
        JTextField searchField = new JTextField("搜索");
        searchField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        searchField.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(200, 200, 200)),
            BorderFactory.createEmptyBorder(8, 15, 8, 15)
        ));
        searchField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));
        topPanel.add(searchField);
        
        // 分类标签
        JPanel categoryPanel = new JPanel(new GridLayout(1, 4, 10, 0));
        categoryPanel.setBackground(new Color(240, 240, 240));
        
        String[] categories = {"手机数码", "美妆服饰", "游戏交易", "潮流玩物"};
        for (String category : categories) {
            JLabel label = new JLabel(category, JLabel.CENTER);
            label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
            label.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
            categoryPanel.add(label);
        }
        
        topPanel.add(categoryPanel);
        return topPanel;
    }

    private JPanel createProductGrid(CardLayout cardLayout, JPanel cardPanel) {
        JPanel productGrid = new JPanel(new GridLayout(3, 3, 15, 15));
        productGrid.setBackground(Color.WHITE);
        productGrid.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));

        for (int i = 1; i <= 9; i++) {
            JButton productButton = new JButton("<html><center>商品 " + i + "<br>¥899.00</center></html>");
            productButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
            productButton.setBackground(Color.WHITE);
            productButton.setBorder(BorderFactory.createLineBorder(new Color(220, 220, 220)));
            productButton.setPreferredSize(new Dimension(100, 100));
            productButton.addActionListener(e -> cardLayout.show(cardPanel, "productDetail"));
            
            // 模拟商品图片
            productButton.setIcon(new ImageIcon(createProductImage()));
            productButton.setHorizontalTextPosition(SwingConstants.CENTER);
            productButton.setVerticalTextPosition(SwingConstants.BOTTOM);
            
            productGrid.add(productButton);
        }
        return productGrid;
    }

    private Image createProductImage() {
        // 创建一个简单的商品图片
        int size = 60;
        Image image = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setColor(new Color(200, 230, 255));
        g2d.fillRect(0, 0, size, size);
        g2d.setColor(Color.BLUE);
        g2d.drawString("商品", size/2-15, size/2);
        g2d.dispose();
        return image;
    }

    private JPanel createBottomNav() {
        JPanel bottomNav = new JPanel(new GridLayout(1, 6, 5, 0));
        bottomNav.setBackground(new Color(250, 250, 250));
        bottomNav.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(230, 230, 230)));

        String[] navItems = {"官方集", "集市", "发布", "ERYIJI", "收藏", "我的"};
        for (String item : navItems) {
            JButton button = new JButton(item);
            button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            button.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
            button.setBackground(new Color(250, 250, 250));
            bottomNav.add(button);
        }
        return bottomNav;
    }
}